package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 退卡交易明细表实体类
 * 作者: 
 * 日期：2018-08-14 20:04:51
 * 描述：
 */
public class TradeCardRefund extends TradeComm implements Serializable{

	private static final long serialVersionUID = 1L;
	  
    /*
     * 主表ID
     */
    private String commId;
    
    /*
     * 审核员ID
     */
    private String operId;
    
    /*
     * 姓名
     */
    private String name;
    
    /*
     * 性别
     */
    private String gender;
    
    /*
     * 证件号码
     */
    private String idNumber;
    
    /*
     * 证件类型
     */
    private String idType;
    
    /*
     * 客户PIN密文
     */
    private String pin;
    
    /*
     * 有效起始时间
     */
    private String startTime;
    
    /*
     * 有效终止时间
     */
    private String endTime;
    
    /*
     * 信用标志
     */
    private String creditFlag;
    
    /*
     * 积分
     */
    private Long points;
    
    /*
     * 前次交易全局设备ID
     */
    private String exDeviceId;
    
    /*
     * 前次交易类型
     */
    private String exTradeCode;
    
    /*
     * 前次交易时间
     */
    private String exTradeTime;
    
    /*
     * 前次交易金额
     */
    private Long exTradeMoney;
    
    /*
     * 前次交易操作员号
     */
    private String exOper;
    
    /*
     * 透支金额
     */
    private Long overdraftMoney;
    
    /*
     * 售票站点
     */
    private String saleAddress;
    
    
    
    /**
     * 默认构造方法
     **/
    public TradeCardRefund(){
      super();
    }
    
    /** 
     * getger和setter方法
     */
    
    public String getCommId(){
      return commId;
    }
    public void setCommId(String commId){
      this.commId=commId;
    }
    
    public String getOperId(){
      return operId;
    }
    public void setOperId(String operId){
      this.operId=operId;
    }
    
    public String getName(){
      return name;
    }
    public void setName(String name){
      this.name=name;
    }
    
    public String getGender(){
      return gender;
    }
    public void setGender(String gender){
      this.gender=gender;
    }
    
    public String getIdNumber(){
      return idNumber;
    }
    public void setIdNumber(String idNumber){
      this.idNumber=idNumber;
    }
    
    public String getIdType(){
      return idType;
    }
    public void setIdType(String idType){
      this.idType=idType;
    }
    
    public String getPin(){
      return pin;
    }
    public void setPin(String pin){
      this.pin=pin;
    }
    
    public String getStartTime(){
      return startTime;
    }
    public void setStartTime(String startTime) throws ParseException{
    	checkTimeStyle(startTime, "StartTime");
      this.startTime=startTime;
    }
    
    public String getEndTime(){
      return endTime;
    }
    public void setEndTime(String endTime) throws ParseException{
    	checkTimeStyle(endTime, "EndTime");
      this.endTime=endTime;
    }
    
    public String getCreditFlag(){
      return creditFlag;
    }
    public void setCreditFlag(String creditFlag){
      this.creditFlag=creditFlag;
    }
    
    public Long getPoints(){
      return points;
    }
    public void setPoints(Long points){
      this.points=points;
    }
    
    public String getExDeviceId(){
      return exDeviceId;
    }
    public void setExDeviceId(String exDeviceId){
      this.exDeviceId=exDeviceId;
    }
    
    public String getExTradeCode(){
      return exTradeCode;
    }
    public void setExTradeCode(String exTradeCode){
      this.exTradeCode=exTradeCode;
    }
    
    public String getExTradeTime(){
      return exTradeTime;
    }
    public void setExTradeTime(String exTradeTime){
      this.exTradeTime=exTradeTime;
    }
    
    public Long getExTradeMoney(){
      return exTradeMoney;
    }
    public void setExTradeMoney(Long exTradeMoney){
      this.exTradeMoney=exTradeMoney;
    }
    
    public String getExOper(){
      return exOper;
    }
    public void setExOper(String exOper){
      this.exOper=exOper;
    }
    
    public Long getOverdraftMoney(){
      return overdraftMoney;
    }
    public void setOverdraftMoney(Long overdraftMoney){
      this.overdraftMoney=overdraftMoney;
    }
    
    public String getSaleAddress(){
      return saleAddress;
    }
    public void setSaleAddress(String saleAddress){
      this.saleAddress=saleAddress;
    }
    
    public String getId(){
      return id;
    }
    public void setId(String id){
      this.id=id;
    }
    
    /**
     * 根据版本容解析交易内容
     * @param message
     * @throws Exception 
     */
	public TradeCardRefund init(String message) throws Exception {

        TradeMessageParser messageParser = null;
        if(message.startsWith("SZMETROV2")) {

            // 根据报文格式长度来定义， -1为行业分割符
            int[] splitNumber = {
                    9, 8, 9, 2, 10, 9, 6, 6, 14, 10, 8, 8, 8, 8, 8, 5, 10, 16, 1, 20, 2, 16, 14, 14, 2, 8, 9, 2, 14, 8, 6, 20, 10, 8, 2, 2, 16, 1, 1, 10, 2, 2, 2, 4, 4, 1, 4, 5, -1, 5
            };

            messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));
			
			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));
			
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(10)));
			this.setTradeValue(Long.parseLong(messageParser.getFiled(11)));
			this.setBalance(Long.parseLong(messageParser.getFiled(12)));	//卡余额
			this.setDeposit(Long.parseLong(messageParser.getFiled(13)));
			this.setCost(Long.parseLong(messageParser.getFiled(14)));
			
			this.setCardSeq(Long.parseLong(messageParser.getFiled(15)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(16)));
			this.setName(messageParser.getFiled(17));
			this.setGender(messageParser.getFiled(18));
			this.setIdNumber(messageParser.getFiled(19));
			
			this.setIdType(messageParser.getFiled(20));
			this.setPin(messageParser.getFiled(21));
			this.setStartTime(messageParser.getFiled(22));
			this.setEndTime(messageParser.getFiled(23));
			this.setCreditFlag(messageParser.getFiled(24));
			
			this.setPoints(Long.parseLong(messageParser.getFiled(25)));
			this.setExDeviceId(messageParser.getFiled(26));
			this.setExTradeCode(messageParser.getFiled(27));
			this.setExTradeTime(messageParser.getFiled(28));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(29)));
			
			this.setExOper(messageParser.getFiled(30));
			this.setSamcardId(messageParser.getFiled(31));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(32)));
			this.setOverdraftMoney(Long.parseLong(messageParser.getFiled(33)));
			this.setCardCode(messageParser.getFiled(34));
			
			this.setTradeSub(messageParser.getFiled(35));
			this.setPhysicalId(messageParser.getFiled(36));
			this.setTestFlag(messageParser.getFiled(37));
			this.setGreyFlag(messageParser.getFiled(38));
			this.setSaleAddress(messageParser.getFiled(39));
			
			this.setMedia(messageParser.getFiled(40));
			this.setCardVersion(messageParser.getFiled(41));
			this.setSubCardCode(messageParser.getFiled(42));
			this.setCityCode(messageParser.getFiled(43));
			this.setBusinessCode(messageParser.getFiled(44));
			
			this.setIndustrySplit(messageParser.getFiled(45));
			this.setIssuer(messageParser.getFiled(46));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(47)));
			super.setIndustryData(messageParser.getFiled(48));
			super.setCrc16(messageParser.getFiled(49));

        } else if(message.startsWith("SZMETROV3")) {

            /** 根据报文格式长度来定义， -1为行业分割符
             * 一、V3卡号由9位变为20位
             * 二、增加字段：
             付费方式
             支付方式
             发卡方机构码
             发卡方交易流水号
             运营模式
             三、票卡发行方标识	从长度4变为长度2
             */
            int[] splitNumber = {
                    9, 8, 20, 2, 10,   9, 6, 6, 14, 10,   8, 8, 8, 8, 8,   5, 10, 16, 1, 20,   2, 16, 14, 14, 2,
                    8, 9, 2, 14, 8,   6, 20, 10, 8, 2,   2, 16, 1, 1, 10,   2, 2, 2, 4, 4,
                    1, 2, 8, 10, 4,    //V3增加的字段
                    1, 2, 5,
                    -1, 5};
            messageParser = new TradeMessageParser(message, splitNumber);
            this.setVersion(messageParser.getFiled(0));
            this.setTac(messageParser.getFiled(1));
            this.setCardId(messageParser.getFiled(2));
            this.setTradeCode(messageParser.getFiled(3));
            this.setTradeAddress(messageParser.getFiled(4));

            this.setDeviceId(messageParser.getFiled(5));
            this.setOperator(messageParser.getFiled(6));
            this.setOperId(messageParser.getFiled(7));
            this.setTradeTime(messageParser.getFiled(8));
            this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

            this.setTradeMoney(Long.parseLong(messageParser.getFiled(10)));
            this.setTradeValue(Long.parseLong(messageParser.getFiled(11)));
            this.setBalance(Long.parseLong(messageParser.getFiled(12)));	//卡余额
            this.setDeposit(Long.parseLong(messageParser.getFiled(13)));
            this.setCost(Long.parseLong(messageParser.getFiled(14)));

            this.setCardSeq(Long.parseLong(messageParser.getFiled(15)));
            this.setDeviceSum(Long.parseLong(messageParser.getFiled(16)));
            this.setName(messageParser.getFiled(17));
            this.setGender(messageParser.getFiled(18));
            this.setIdNumber(messageParser.getFiled(19));

            this.setIdType(messageParser.getFiled(20));
            this.setPin(messageParser.getFiled(21));
            this.setStartTime(messageParser.getFiled(22));
            this.setEndTime(messageParser.getFiled(23));
            this.setCreditFlag(messageParser.getFiled(24));

            this.setPoints(Long.parseLong(messageParser.getFiled(25)));
            this.setExDeviceId(messageParser.getFiled(26));
            this.setExTradeCode(messageParser.getFiled(27));
            this.setExTradeTime(messageParser.getFiled(28));
            this.setExTradeMoney(Long.parseLong(messageParser.getFiled(29)));

            this.setExOper(messageParser.getFiled(30));
            this.setSamcardId(messageParser.getFiled(31));
            this.setSamcardSeq(Long.parseLong(messageParser.getFiled(32)));
            this.setOverdraftMoney(Long.parseLong(messageParser.getFiled(33)));
            this.setCardCode(messageParser.getFiled(34));

            this.setTradeSub(messageParser.getFiled(35));
            this.setPhysicalId(messageParser.getFiled(36));
            this.setTestFlag(messageParser.getFiled(37));
            this.setGreyFlag(messageParser.getFiled(38));
            this.setSaleAddress(messageParser.getFiled(39));

            this.setMedia(messageParser.getFiled(40));
            this.setCardVersion(messageParser.getFiled(41));
            this.setSubCardCode(messageParser.getFiled(42));
            this.setCityCode(messageParser.getFiled(43));
            this.setBusinessCode(messageParser.getFiled(44));

            // V3增加字段
            this.setPayMethod(messageParser.getFiled(45));
            this.setPayType(messageParser.getFiled(46));
            this.setIssuerCode(messageParser.getFiled(47));
            this.setIssuerSeq(messageParser.getFiled(48));
            this.setOperMode(messageParser.getFiled(49));

            this.setIndustrySplit(messageParser.getFiled(50));
            this.setIssuer(messageParser.getFiled(51));
            super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(52)));
            super.setIndustryData(messageParser.getFiled(53));
            super.setCrc16(messageParser.getFiled(54));
        }
		return this;
	}
}                    